import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:hbtrans_flutter/routes.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../apis.dart';
import '../const.dart';

class SignInfo extends StatefulWidget {
  @override
  _SignInfoState createState() => _SignInfoState();
}

class _SignInfoState extends State<SignInfo> {
//  TextEditingController _nameController =
//      TextEditingController.fromValue(TextEditingValue(text: ''));
//
//  TextEditingController _idController =
//      TextEditingController.fromValue(TextEditingValue(text: ''));
//
//  TextEditingController _phoneController =
//  TextEditingController _carController =//      TextEditingController.fromValue(TextEditingValue(text: ''));
//      TextEditingController.fromValue(TextEditingValue(text: ''));
//
//  TextEditingController _typeController;

  var _scaffoldKey = GlobalKey<ScaffoldState>();

//  var classes = ['A1', 'A2', 'A3', 'B1', 'B2', 'C1', 'C2', 'C3', 'C4', 'C5'];
  var traintype = '请选择车型';
  var _userInfo = Map<String, dynamic>();
  var inscode, busiscope = '';
  List<String> classes = List();

  _showPickerIcons(BuildContext context) {
    Picker(
      adapter: PickerDataAdapter(data: _setPickerItems()),
      title: Text('请选择'),
      cancelText: '取消',
      confirmText: '确定',
      selectedTextStyle: TextStyle(color: Colors.blue),
      onConfirm: (Picker picker, List value) {
        setState(() {
          traintype = classes[value[0]];
          _userInfo["car_type"] = traintype;
        });
        print(traintype);
      },
    ).show(_scaffoldKey.currentState);
  }

  _setPickerItems() {
    List<PickerItem<dynamic>> pickerItems = List();
    for (String s in classes) {
      pickerItems.add(PickerItem(
          value: Icons.add,
          text: Text(s,
              style: TextStyle(color: const Color(0xff333333), fontSize: 16))));
    }
    return pickerItems;
  }

  //提交
  _submit() {
    _userInfo['inscode'] = inscode;
    MyDio.getInstance().clearInstance();
    MyDio.getInstance(url: Const.TRANS_SERVER_MY_PATH).get(Apis.signSave,
        (data) {
      print(data);
      setState(() {
        String route = '${Routes.pay}';
        Routes.router.navigateTo(context, route,
            replace: false, transition: TransitionType.fadeIn);
        _scaffoldKey.currentState.showSnackBar(SnackBar(
            elevation: 0,
            backgroundColor: Color(0xff23AFDC),
            content: Text(
              '保存成功',
              style: TextStyle(fontSize: 16),
            )));
        Navigator.pop(context);
      });
    }, params: _userInfo);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getSign();
  }

  _getSign() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    inscode = preferences.getString('inscode');
    busiscope = preferences.getString('busiscope');
    classes = busiscope.split(',');
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        backgroundColor: const Color(0xff23AFDC),
        elevation: 0,
        title: Text('报名学车'),
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            flex: 9,
            child: ListView(
              shrinkWrap: true,
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.all(20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        '顾城县天安机动车与驾驶员驾驶员培训学校',
                        style: TextStyle(color: Colors.black, fontSize: 16),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Text(
                        '培训类型：$busiscope',
                        style: TextStyle(color: Colors.black54, fontSize: 14),
                      ),
                      SizedBox(
                        height: 15,
                      ),
                      Container(
                        height: 10,
                        color: Colors.grey[100],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '姓名',
                            style: TextStyle(color: Colors.black, fontSize: 14),
                          ),
                          SizedBox(
                            width: 50,
                          ),
                          Expanded(
                            child: TextFormField(
                              style:
                                  TextStyle(color: Colors.black, fontSize: 14),
                              controller: TextEditingController(
                                  text: _userInfo['user_name']),
                              onFieldSubmitted: (value) {
                                _userInfo['user_name'] = value;
                              },
                              decoration: InputDecoration(
                                hintText: '请输入姓名',
                                border: InputBorder.none, //去掉下划线
                              ),
                            ),
                          )
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '身份证号',
                            style: TextStyle(color: Colors.black, fontSize: 14),
                          ),
                          SizedBox(
                            width: 26,
                          ),
                          Expanded(
                            child: TextFormField(
                              style:
                                  TextStyle(color: Colors.black, fontSize: 14),
                              controller: TextEditingController(
                                  text: _userInfo['card_no']),
                              onFieldSubmitted: (value) {
                                _userInfo['card_no'] = value;
                              },
                              decoration: InputDecoration(
                                hintText: '请输入身份证号',
                                border: InputBorder.none, //去掉下划线
                              ),
                            ),
                          )
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '联系电话',
                            style: TextStyle(color: Colors.black, fontSize: 14),
                          ),
                          SizedBox(
                            width: 26,
                          ),
                          Expanded(
                            child: TextFormField(
                              style:
                                  TextStyle(color: Colors.black, fontSize: 14),
                              controller: TextEditingController(
                                  text: _userInfo['phone']),
                              onFieldSubmitted: (value) {
                                _userInfo['phone'] = value;
                              },
                              decoration: InputDecoration(
                                hintText: '请输入联系电话',
                                border: InputBorder.none, //去掉下划线
                              ),
                            ),
                          )
                        ],
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Expanded(
                            child: Row(
                              children: <Widget>[
                                Text(
                                  '申请车型',
                                  style: TextStyle(
                                      color: Colors.black, fontSize: 14),
                                ),
                                SizedBox(
                                  width: 26,
                                ),
                                Expanded(
                                  child: GestureDetector(
                                    onTap: () {
                                      _showPickerIcons(context);
                                    },
                                    child: Text(
                                      traintype,
                                      style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: 14),
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                          Image.asset(
                            'images/icon_sign_xiala.png',
                            height: 16,
                            width: 16,
                          )
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 1,
            child: Padding(
              padding: EdgeInsets.fromLTRB(15, 0, 15, 15),
              child: _button(),
            ),
          ),
        ],
      ),
    );
  }

  _button() {
    return MaterialButton(
      minWidth: MediaQuery.of(context).size.width,
      elevation: 0,
      onPressed: () {
        _submit();
      },
      child: Text(
        "提交",
        style: TextStyle(color: Colors.black, fontSize: 16),
      ),
      color: Color(0xff999999),
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(20))),
    );
  }
}
